/**
 * Created by zhanglihe on 16/1/23.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function ($, common, ready, Collection, template, Model, dialog) {

    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'custom_category_index',
                model: {
                    url: {
                        delete: _global.url.api + 'custom_category'
                    },
                    event: {
                        '.item-edit': 'edit',
                        '.item-remove': 'remove',
                        '.item-status:click': 'status'
                    },
                    edit: function (e) {
                        var __this = this;
                        e.preventDefault();
                        dialog({
                            title: '修改：' + __this.data.name,
                            width: 800,
                            content: template('add-tpl', __this.data),
                            ok: function () {
                                var cate_type = $('[name="type"]:checked').val();
                                var config_cate = new Array();
                                $('[name="cate-' + cate_type + '-item"]:checked').each(function () {
                                    var cate_id = $.trim($(this).val());
                                    config_cate.push(cate_id);
                                });
                                var data = {
                                    id: __this.data.id,
                                    listorder: $('[name="listorder"]').val(),
                                    name: $('[name="name"]').val(),
                                    config_cate_type: cate_type,
                                    config_cate: config_cate
                                };
                                if (data.name == '' || !data.listorder) {
                                    Alert({'msg': '请正确填写', 'type': 'error'});
                                    return false;
                                }
                                if (new Model({
                                        url: _global.url.api + 'custom_category',
                                        data: data
                                    }).put()) {
                                    _this.collection.reload();
                                    return true;
                                }
                                return false;
                            }, cancel: function () {
                            }
                        }).show();
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var _this = this;
                        dialog({
                            content: '删除后无法恢复(同时删除相关商品关系)，确定要删除吗?',
                            ok: function () {
                                _this.del();
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    status: function (e) {
                        e.preventDefault();
                        var _this = this;
                        $.ajax({
                            url: _global.url.api + 'custom_category',
                            type: 'put',
                            data: {'id': _this.data.id, 'act': 'status', 'status': _this.data.status == 1 ? 0 : 1},
                            success: function (res) {
                                if (res.code == 0) {
                                    _this.data.status = _this.data.status == 1 ? 0 : 1;
                                    _this.create_element();
                                }
                            },
                            error: function () {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    }
                },
                callback: function () {
                    loaded();//移除加载效果
                    $('[name=use_custom_catagory][value=' + _this.collection.result._use_custom_catagory + ']').prop("checked", true);
                    $('#btn-add').show();
                }
            });
            this.collection.fetch();
        },
        event: function () {
            var _this = this;
            $('body').on('click', '[name=type]', function (e) {
                $('.cate-item').hide();
                $('.cate-' + $(this).val()).show();
            });
            $('body').on('click', '[name=use_custom_catagory]', function (e) {
                var use_custom_catagory = $(this).val();
                if (use_custom_catagory == _this.collection.result._use_custom_catagory) {
                    return true;
                }
                if (new Model({
                        url: _global.url.api + 'custom_category',
                        data: {'id': 1, 'act': 'set_use_custom_catagory', 'use_custom_catagory': use_custom_catagory}
                    }).put()) {
                    _this.collection.result._use_custom_catagory = use_custom_catagory;
                    return true;
                }
                return false;
            });
            $('#btn-add').on('click', function (e) {
                e.preventDefault();
                dialog({
                    title: '增加自定义分类',
                    width: 800,
                    content: template('add-tpl', {
                        config_cate: {
                            'type': 2,
                            cate_second: _this.collection.result._site_cate_second,
                            cate_third: _this.collection.result._site_cate_third
                        }
                    }),
                    ok: function () {
                        var cate_type = $('[name="type"]:checked').val();
                        var config_cate = new Array();
                        $('[name="cate-' + cate_type + '-item"]:checked').each(function () {
                            var cate_id = $.trim($(this).val());
                            config_cate.push(cate_id);
                        });
                        var data = {
                            listorder: $('[name="listorder"]').val(),
                            name: $('[name="name"]').val(),
                            config_cate_type: cate_type,
                            config_cate: config_cate
                        };
                        if (data.name == '' || !data.listorder) {
                            Alert({'msg': '请正确填写', 'type': 'error'});
                            return false;
                        }
                        if (new Model({
                                url: _global.url.api + 'custom_category',
                                data: data
                            }).post()) {
                            _this.collection.reload();
                            return true;
                        }
                        return false;
                    }, cancel: function () {
                    }
                }).show();
            });
        }
    };
    new index();
});